package com.zfei.gfb.repo;

import com.zfei.gfb.api.domain.EventTuple;
import com.zfei.gfb.api.domain.InboxTuple;
import com.zfei.gfb.api.domain.TalkEvent;
import com.zfei.gfb.dao.TalkEventDao;
import com.zfei.gfb.model.TalkEventEntity;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author fgh
 */
@Service
public class TalkRepo {

    @Resource
    private TalkEventDao talkEventDao;


    public TalkEventDao getTalkEventDao() {
        return talkEventDao;
    }

    @Transactional
    public int createEvent(TalkEvent event) {
        long now = Now.millis();
        event.setDeleteTime(0L);
        event.setCreateTime(now);
        event.setUpdateTime(now);

        TalkEventEntity entity = new TalkEventEntity();
        BeanUtils.copyProperties(event, entity);
        return this.talkEventDao.save(entity);
    }

    @Transactional
    public int updateEvent(TalkEvent event) {
        TalkEventEntity entity = new TalkEventEntity();
        BeanUtils.copyProperties(event, entity);
        return this.talkEventDao.update(entity);
    }

    public Map<String, TalkEvent> getEventMap(Collection<String> eventIds) {
        Map<String, TalkEvent> result = new HashMap<>();
        if (eventIds != null && eventIds.size() > 0) {
            List<TalkEventEntity> entities = this.talkEventDao.list(eventIds);
            if (entities != null && entities.size() > 0) {
                for (TalkEventEntity entity : entities) {
                    if (entity != null) {
                        TalkEvent event = Beans.copy(entity, TalkEvent.class);
                        result.put(event.getEventId(), event);
                    }
                }
            }
        }
        return result;
    }

    public TalkEvent getEvent(String eventId) {
        if (StringUtils.isBlank(eventId)) {
            return null;
        }

        TalkEventEntity entity = this.talkEventDao.get(eventId);
        return entity != null ? Beans.copy(entity, TalkEvent.class) : null;
    }

    public List<TalkEvent> getEvent(Collection<String> eventIds) {
        List<TalkEvent> result = new ArrayList<>();
        if (eventIds != null && eventIds.size() > 0) {
            Map<String, TalkEvent> eventMap = this.getEventMap(eventIds);
            for (String eventId : eventIds) {
                TalkEvent event = eventMap.get(eventId);
                if (event != null) {
                    result.add(event);
                }
            }
        }
        return result;
    }

    public List<TalkEvent> getEventByEventTuples(Collection<EventTuple> tuples) {
        if (tuples == null || tuples.isEmpty()) {
            return new ArrayList<>();
        }

        Map<String, EventTuple> tupleMap = new LinkedHashMap<>();
        for (EventTuple tuple : tuples) {
            if (tuple != null) {
                tupleMap.put(tuple.getEventId(), tuple);
            }
        }

        if (tupleMap.isEmpty()) {
            return new ArrayList<>();
        }

        List<TalkEvent> events = this.getEvent(tupleMap.keySet());
        for (TalkEvent event : events) {
            event.setEventTime(tupleMap.get(event.getEventId()).getEventTime());
        }
        return events;
    }

    public List<TalkEvent> getEventByInboxTuples(List<InboxTuple> tuples) {
        if (tuples == null || tuples.isEmpty()) {
            return new ArrayList<>();
        }

        Map<String, InboxTuple> tupleMap = new LinkedHashMap<>();
        for (InboxTuple tuple : tuples) {
            if (tuple != null) {
                tupleMap.put(tuple.getEventId(), tuple);
            }
        }

        if (tupleMap.isEmpty()) {
            return new ArrayList<>();
        }

        List<TalkEvent> events = this.getEvent(tupleMap.keySet());
        for (TalkEvent event : events) {
            event.setEventTime(tupleMap.get(event.getEventId()).getEventTime());
        }
        return events;
    }

}
